package com.dragon.mmochat.message;

import java.util.ArrayList;
import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.map.MmochatMapPoint;
import com.dragon.mmochat.model.role.MmochatNpc;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.task.MmochatTask;
import com.dragon.mmochat.service.MmochatMapService;
import com.dragon.mmochat.service.MmochatNpcService;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_4_13S extends SkymobiProtocolMessage {
	MmochatPlayer me;
	List<MmochatTask> canAcceptTaskList = new ArrayList<MmochatTask>(); // 可接任务

	public Mmochat_4_13S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_4_13);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		// 可接任务:任务ID、任务名、接任务NPC、NPC所在mapId,x,y
		out.putShort((short) canAcceptTaskList.size());
		for (MmochatTask task : canAcceptTaskList) {
			// 任务ID
			out.putInt(task.getTaskId());
			// 任务名
			out.putPrefixedString(task.getTaskName(), 1);

			// 接任务NPC名字
			MmochatNpc npc = MmochatNpcService.getNpcById(task
					.getAccept_npcId());
			if (npc != null) {
				out.putPrefixedString(npc.getName(), 1);
			} else {
				out.putPrefixedString("-", 1);
			}

			// 任务寻路信息
			MmochatMapPoint guider = task.getGuiderPoint(me);
			if (guider != null) {
				out.putInt(guider.getMapId());
				out.putShort((short) guider.getX());
				out.putShort((short) guider.getY());
			} else {
				out.putInt(-1);
				out.putShort((short) 0);
				out.putShort((short) 0);
			}
		}
	}

	public void setCanAcceptTaskList(List<MmochatTask> canAcceptTaskList) {
		this.canAcceptTaskList = canAcceptTaskList;
	}

	public void addCanAcceptTaskList(MmochatTask t) {
		canAcceptTaskList.add(t);
	}

	public void setMe(MmochatPlayer me) {
		this.me = me;
	}

}